<?php
namespace xptml;

class Cache
{
	private static $self = null;

    private function __construct()
    {

    }

	static function get(& $variable, \Closure $onIniti = null)
	{
		static $table = array();

		if (is_null(self::$self)) self::$self = new self();

		list(, $caller) = debug_backtrace();

		$key = $variable;
		$variable = null;
		$hash = sha1(@$caller['class'].@$caller['type'].$caller['function']);

		if (file_exists($filename = CACHE . "{$hash}.php") && ! isset($table[$hash]))
		{
			$table[$hash] = require_once($filename);
		}

		if (isset($table[$hash]))
		{
			$variable = $table[$hash][$key];
		}

		if (is_callable($onIniti) && is_null($variable))
		{
			$table[$hash][$key] = $variable = $onIniti();
			file_put_contents($filename, '<?php return ' . var_export($table[$hash], true) . '; ?'.'>');
			return false;
		}
	}
}

?>
